package com.ssm.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hqj.ReturnUtil;
import com.ssm.dao.MenuDao;
import com.ssm.entity.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class MenuServiceI implements BaseService<Menu> {
    @Autowired
    private MenuDao md;
    @Override
    public Menu get(Menu menu) {
        List<Menu> list = md.find(menu);
        return list.size()>0?list.get(0):new Menu();
    }

    @Override
    public List<Menu> find(Menu menu) {
        List<Menu> list = md.find(menu);
        return list;
    }

    @Override
    public Map<String, Object> add(Menu menu) {
        int i = md.add(menu);
        return ReturnUtil.returnMap(i>0,"新增");
    }

    @Override
    public Map<String, Object> edit(Menu menu) {
        int i = md.edit(menu);
        return ReturnUtil.returnMap(i>0,"编辑");
    }

    @Override
    public Map<String, Object> remove(Menu menu) {
        int i = md.remove(menu.getId());
        return ReturnUtil.returnMap(i>0,"删除");
    }






    public Map<String,Object> batchRemove(Menu menu){
        //获取所有的菜单
        List<Menu> menus = this.find(new Menu());
        List<Integer> ids = new ArrayList<Integer>();
        ids = getIds(menu.getId(),menus,ids);
        int i = md.batchRemove(ids);
        return ReturnUtil.returnMap(i>0,"删除");
    }

    public List<Integer> getIds(int id,List<Menu> menus,List<Integer> ids){
        ids.add(id);
        for(Menu menu:menus){
            if(menu.getParentId() == id){
                getIds(menu.getId(),menus,ids);
            }
        }
        return ids;
    }


    public String getMenuByRoleId(int roleId){
        List<Menu> list = md.getMenuByRoleId(roleId);

        return createMenu(list);
    }

    public String createMenu(List<Menu> list){
        /*
        [{
            menuName:'系统菜单',id:1,parentId:0,
            children:[{
                menuName:'用户管理',id:3,parentId:1,
                children:[{},{}]
            },{
                menuName:'菜单管理',id:4,parentId:1,
            },{
                menuName:'角色管理',id:5,parentId:1,
            }]
        },{
            menuName:'文件管理',id:2,parentId:0,
        }]
         */
        //创建一个json数组
        JSONArray rootArr = new JSONArray();
        //遍历list集合
        for(Menu m:list){
            //找到最上层目录
            if(m.getParentId()==0){
                rootArr.add(createBranch(list,m));
            }
        }

        return rootArr.toString();
    }

    public JSONObject createBranch(List<Menu> list,Menu m){
        //把跟菜单转换为字符串
        String root = JSON.toJSONString(m);
        //把字符串转换为jsonObject
        JSONObject jo = JSON.parseObject(root);
        //创建json数组存储该跟路径的子菜单
        JSONArray ja = new JSONArray();
        for(Menu menu:list){
            if(m.getId() == menu.getParentId()){
                ja.add(menu);
            }
        }
        jo.put("children",ja);
        return jo;
    }

    public Map<String,List<String>> getBtnByRoleId(int roleId){
        Map<String,List<String>> map = new HashMap<String, List<String>>();
        List<String> list = md.getBtnByRoleId(roleId);
        /*
          [uc/adduser,uc/editUser,uc/removerUser] ➡ [uc,addUser]    [uc,editUser]   [uc,removeUser]
                         ⬇                               ⬇
          uc:[addUser,editUser,removeUser]            uc:[addUser]
         */
        for(String str:list){
            String strs[] = str.split("/");
            String key = strs[0],value = strs[1];
            List<String> ls = new ArrayList<String>();
            if(map.containsKey(key)){
                ls = map.get(key);
            }
            ls.add(value);
            map.put(key,ls);

        }
        return map;
    }

}
